var app = getApp()
var config = require('../../config.js')
var http = require('../../utils/httpHelper.js')
Page({
  data: {
    isLogin:false,
    userInfo: {},
  },
  onShow:function (){
    var that = this;
    let userInfo = wx.getStorageSync('user');
    if (userInfo!=null&&userInfo!=undefined&&userInfo!={}&&userInfo!='') {
      that.setData({
        isLogin:true,
        userInfo:userInfo
      })
    }
  },
  userdata:function (){
      wx.navigateTo({url: "/pages/userdata/index"})
  },
  address: function (){
      wx.navigateTo({url:"/pages/address/index"});
  },
  dating:function (){
    wx.navigateTo({url: "/pages/dating/index"})
  },
  order:function (){
    wx.navigateTo({url: "/pages/order/index"})
  },
  market:function (){
    wx.navigateTo({url: "/pages/market/index"})
  },
  shoppingcar:function (){
    wx.navigateTo({url: "/pages/shoppingcar/index"})
  },
  getPhoneNumber(e) {
    if(e.detail.errMsg == 'getPhoneNumber:fail user deny'){//拒绝获取手机号
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '未授权',
        success: function (res) {}
      })
    }else if(e.detail.errMsg.indexOf('getPhoneNumber:fail Error')>=0){//不支持获取手机号
    }else {//同意获取手机号
      var that = this;
      wx.login({
        success: function (resLogin) {
          let code = resLogin.code;
          wx.getUserInfo({
            success: function (res) {
              that.setData({
                isLogin:true,
                userInfo:res.userInfo
              })
              http.putOpenid(code, (resOpen, err) => {
                if (resOpen.data.userId) {
                  app.setUserInfo(resOpen.data);
                  setTimeout(()=>{
                    that.data.userInfo = wx.getStorageSync('user');
                    that.onShow();
                  }, 2000);
                }else {
                  let openId = resOpen.data.openid;
                  let inviteId = wx.getStorageSync('inviteId');
                  http.httpGet(config.clubApi.getPhone,{
                    appid:config.APPID,
                    encryptedData: e.detail.encryptedData,
                    iv: e.detail.iv,
                    sessionKey: resOpen.data.sessionKey,
                    openid: openId,
                    inviteId: inviteId==null||inviteId==''?0:inviteId
                  },function(res){
                    if(res.code == '200'){
                      wx.setStorageSync('inviteId', null);
                      app.queryUser(openId);
                      setTimeout(()=>{
                        that.data.userInfo = wx.getStorageSync('user');
                        that.onShow();
                      }, 2000);
                    }else {
                      that.setData({
                        isLogin:false,
                        userInfo:{}
                      })
                    }
                  });
                }
              });
            }
          })
        }
      })
    }
  },
  // 退出登录
  loginOut() {
    wx.request({
      method:'GET',
      url: config.BASE_URL + config.clubApi.logout,
      header: {
        "U-TOKEN": wx.getStorageSync('user') ? wx.getStorageSync('user').token : config.clubApi.logout,
        "userId": wx.getStorageSync('user').userId
      },
      success:(res) => {
        if(res.data.code == '200'){
          this.setData({
            isLogin:false,
            userInfo:{}
          })
          // 清空缓存
          wx.setStorageSync('user',null);
        }
      }      
    })        
  }
})